#include <iostream>
using namespace std;
#include "LinkList.cpp"
#include <string.h>
//copy
//重载=
//把类变成了 可复制 深拷贝
struct Teacher
{
public:
        char name[64];
        int age;
        char *pdes;
        Teacher(){
                memset(name,0,sizeof(name));
                age = 0;
                pdes = NULL;
        }
        ~Teacher(){
                if(pdes != NULL){
                     delete pdes;   
                }
        }
        Teacher(Teacher &t){
                pdes = new char[100];
                strcpy(this->pdes, t.pdes);
                //this->name = t.name;
                strcpy(this->name,t.name);
                age = t.age;
        }
        Teacher& operator=(Teacher &t){
                //把之前的内存空间释放掉
                if(pdes){
                        delete pdes;
                        pdes = NULL;
                }
                if(pdes == t.pdes){ //t1=t1
                        return *this;
                }
                pdes = new char[100];
                strcpy(this->pdes, t.pdes);
                //this->name = t.name;
                strcpy(this->name,t.name);
                age = t.age;
                return *this;
        }
};
void main_linklist_play(){
        Teacher t1, t2, t3, tmp;
        t1.age = 31;
        t2.age = 32;
        t3.age = 33;

        LinkList<Teacher> list;
        list.insert(t1, 0);
        list.insert(t2, 0);
        list.insert(t3, 0);

        //测试链表的遍历
        for (int i = 0; i < list.getLen(); i++){
                list.get(i, tmp);
                cout<<tmp.age<<" ";
        }
        cout<<endl;

       list.clear();//清空链表
       
        list.insert(t1, 0);
        list.insert(t2, 0);
        list.insert(t3, 0);

        //测试链表的销毁
        printf("开始删除元素\n");
        while ( list.getLen() > 0 ){
                list.del(0,tmp);
                cout<<tmp.age<<" ";
        }
        cout<<endl;
}
//指针
void main_linklist_play2(){
        Teacher t1, t2, t3;
        Teacher *p1, *p2, *p3, *ptmp;
        
        t1.age = 31;
        t2.age = 32;
        t3.age = 33;

        t1.pdes = new char[100];
        strcpy(t1.pdes, "t1t1t1");
        t2.pdes = new char[100];
        strcpy(t2.pdes, "t2t2t2");
        t3.pdes = new char[100];
        strcpy(t3.pdes, "t3t3t3");

        p1 = &t1;
        p2 = &t2;
        p3 = &t3;

        LinkList<Teacher *> list;
        list.insert(p1, 0);
        list.insert(p2, 0);
        list.insert(p3, 0);

        //测试链表的遍历
        for (int i = 0; i < list.getLen(); i++){
                list.get(i, ptmp);
                cout<<ptmp->age<<" ";
        }
        cout<<endl;

       list.clear();//清空链表
       
        list.insert(p1, 0);
        list.insert(p2, 0);
        list.insert(p3, 0);

        //测试链表的销毁
        printf("开始删除元素\n");
        while ( list.getLen() > 0 ){
                list.del(0,ptmp);
                cout<<ptmp->age<<" ";
        }
        cout<<endl;
}
int main(int argc, char const *argv[])
{
        //main_linklist_play();
        main_linklist_play2();
        return 0;
}
